package com.zhang.second.day06;

import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.streaming.api.functions.source.SourceFunction;
import org.apache.flink.streaming.api.watermark.Watermark;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

/**
 * @title: 处理迟到数据：默认策略
 * @author: zhang
 * @date: 2022/2/1 18:43
 */
public class Example3 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        SingleOutputStreamOperator<String> result = env
                .addSource(new SourceFunction<Integer>() {
                    @Override
                    public void run(SourceContext<Integer> ctx) throws Exception {
                        ctx.collectWithTimestamp(1, 1000L);
                        Thread.sleep(1000L);
                        ctx.collectWithTimestamp(2, 2000L);
                        Thread.sleep(1000L);

                        ctx.emitWatermark(new Watermark(5000L));
                        Thread.sleep(1000L);

                        ctx.collectWithTimestamp(3, 4000L);
                        Thread.sleep(1000L);

                        ctx.collectWithTimestamp(4, 6000L);
                        Thread.sleep(1000L);
                    }

                    @Override
                    public void cancel() {

                    }
                })
                .process(new ProcessFunction<Integer, String>() {
                    @Override
                    public void processElement(Integer value, ProcessFunction<Integer, String>.Context ctx, Collector<String> out) throws Exception {
                        if (ctx.timestamp() < ctx.timerService().currentWatermark()) {
                            //到达的数据的时间戳小于当前算子的水位线，侧流输出
                            ctx.output(
                                    new OutputTag<String>("later") {
                                    },
                                    "迟到元素" + value + "侧流输出"
                            );
                        } else {
                            out.collect("数据" + value + "正常输出");
                        }
                    }
                });

        result.print();
        result.getSideOutput(new OutputTag<String>("later") {
        }).print();

        env.execute();
    }
}
